/* 
 *按照教程学习串口的编程 
 *网址1：http://blog.csdn.net/shanzhizi/article/details/9241393 
 *网址2：http://blog.csdn.net/shui1025701856/article/details/7571686 
 *网址3：http://wenku.baidu.com/link?url=TsZc-rSDq_8ayNdWXYzkJRHLEowtx7OQMiFj_BH4M3VfvDrw4hU785aIciwS1xAmViJmz6pN0ety1_Zv8-AM2OAflOB3-UiMu9RvcSQKrAG 
 */  
//头文件  
#include <stdio.h>    //标准输入输出定义  
#include <stdlib.h>   //标准函数库定义  
#include <unistd.h>   //Unix标准函数定义  
#include <sys/types.h>  
#include <sys/stat.h>  
#include <fcntl.h>    //文件控制定义  
#include <termios.h>  //POSIX中断控制定义  
#include <errno.h>    //错误号定义  
#include <string.h>  
#include "uart_init.h"
#include "test.h"
#include "md5sum.h"
#include "timer.h"

#include <signal.h>
#include <sys/time.h>


#define serial_device "/dev/ttyS0" 
#define heart_time 25 
unsigned char frame_number; 
int fd;

int main(void)  
{  
    int nread,i,n =0,datalen=255, len = 0;  
    char testbuff[]="Hello\r\n";  
    unsigned char readbuff[256];  
    unsigned char recev_data[256];
    int recev_len=0;
    unsigned char sum=0;
    frame_number=0xFF;
/*jiexing*/
    //char prodkey[]="b6617bde59500af0ddda4ae303177706";
    //char devtid[]="637823a42bbe4a15a7c0ffdf4fc44a36";
/*diwen*/
    char prodkey[]="6e5c070752a0a8fd4830da418ddbb20d";
    char devtid[]="0ca300ec62c5483a9a8a470ea042d886";
    char devPriKey[]="e3d2f78618bb493b896929726877064b";
    char randomKey[33]={""};
    char authKey[65]={""};
    MD5_CTX md5;
    unsigned char decrypt[32];

    //unsigned char test_data[69];
    /*
    unsigned char test_hex[5]={0x11,0x22,0x0a,0x55,0x8a};
    unsigned char test_string[10];
    */
    //unsigned char send_data[69];
    /*
    int ret;
    unsigned char test_src[]="4801020a0b0c01020304050607010203040506";
    int src_len=6;
    unsigned char test_dst[12];
    int dst_len=12; 
    int num_int;
    */
    fd=serialport();  
    printf("fd=%d\n",fd); 
    signal(SIGALRM, timer_handler);   
    //尝试先写内容  
    nread=write(fd,testbuff,datalen);  
    if(nread==-1)  
    {  
        printf("write bad\n");  
    }  
    else{  
        printf("the test is ok!\n");  
    }     
    //循环读取串口并输出  



    while(1)  
    {  
        printf("enter the while loop\n");  
        //这个是重点，当时我就是这个问题出错了，每次读之前必须清空一次。  
        bzero(readbuff, sizeof(readbuff));  
        bzero(recev_data,sizeof(recev_data));
        bzero(randomKey,sizeof(randomKey));
        datalen=serial_read(fd,readbuff,512);  
        if(datalen > 0)  
        {  
            if(readbuff[0]=='o'&&readbuff[1]=='f'&&readbuff[2]=='f')  
            {  
                 break;  
            }

            if(readbuff[0]=='t'&&readbuff[1]=='e'&&readbuff[2]=='s'&&readbuff[3]=='t')  
            {  
              
              uartdate_send(fd,"AT+CIPSTART=\"TCP\",\"106.75.138.150\",\"87\"\r\n");     
              uartdate_send(fd,"AT+CIPSEND=138\r\n");
              //uartdate_send(fd,"4845010036653563303730373532613061386664343833306461343138646462623230643063613330306563363263353438336139613861343730656130343264383836f0\r\n");
              
              dev_login(prodkey,devtid,fd);
              /*
              hex2string(test_hex,5,test_string,10);
              uartdate_send(fd,test_string);
              */
            }   
            if(readbuff[0]=='4'&&readbuff[1]=='8')  
            {  
               recev_len=TwoCharToByte(readbuff[2],readbuff[3]);
               string2hex(readbuff,recev_len,recev_data);

                if(checkhexsum(recev_data,recev_len)!=0)
               //if(1)
                {
                    printf("sum==recev_data[recev_len-1]\n");
                    switch(recev_data[2])
                    {
                        case 0x02:
                        //printf("%x\n",recev_data[2] );
                        printf("readbuff=%s\n",readbuff);
                        //uartdate_send(fd,recev_data);
                        strncpy(randomKey,readbuff+8,32);
                        printf("randomKey=%s\n",randomKey);
                        printf("authKey=%s\n",strcat(randomKey,devtid));
                        printf("authKey2=%s\n",strcat(randomKey,devPriKey));
    MD5Init(&md5);
    //MD5Update(&md5,prodkey,strlen((char*)prodkey));
    MD5Update(&md5,randomKey,strlen((char*)randomKey));
    MD5Final(&md5,decrypt);        
     
    //printf("加密前：%s\n加密后：",prodkey);
    printf("加密前：%s\n加密后：",randomKey);
    for(i=0;i<16;i++) {
        printf("%02x",decrypt[i]);
    }
    printf("\n");
    auth_recog(decrypt,fd);
    
                        break;
                        case 0x04:
                        //printf("%x\n",recev_data[2] );
                        report_state(fd);
                        heart_beat(fd); 
                        set_refresh_time(heart_time); 
                        //sleep(3);
                        //heart_beat(fd);

                        break;
                        case 0x06:
                        printf("report_state_successful\n");
                        printf("%x\n",recev_data[2] );
                        break;
                        case 0x08:
                        printf("%x\n",recev_data[2] );
                        break;
                        case 0x0A:
                        printf("%x\n",recev_data[2] );
                        break;
                        case 0x0B:
                        printf("%x\n",recev_data[2] );
                        break;
                    }
                }
                else    
                {
                    printf("sum=%x\n",sum);
                    printf("recev_data[recev_len]=%x\n",recev_data[recev_len-1] );
                }
            }
            //serial_write(fd,readbuff,datalen); 
            //test(); 
        }  
    }  
}  


